	.data
promptOne: .asciiz "Please enter an integer (a):"
promptTwo: .asciiz "Please enter another integer (b):"
product:   .asciiz "Product:"
quotient1: .asciiz "\nQuotient (a/b):"
remOne:	   .asciiz "\nRemainder (a/b):"
quotient2: .asciiz "\nQuotient (b/a):"
remTwo:	   .asciiz "\nRemainder (b/a):"
divByZero: .asciiz "Sorry, divide-by-zero not allowed"
Goodbye:   .asciiz "\nThank you for using the program. Goodbye."

	.text

main:

li $t3, 0	#Product Register
li $t4, 0	#counter

#########################
# Get input from user
#########################
#Prompt for first integer
li $v0, 4
la $a0, promptOne
syscall

#Read first integer
li $v0,5
syscall
move $s0, $v0	#$t0 - multiplier
move $t0, $s0

#Prompt for second integer
li $v0, 4
la $a0, promptTwo
syscall

#Read second Integer
li $v0, 5
syscall
move $s1, $v0	#$t1 - multiplicand
move $t1, $s1

#########################
# Compute a*b
#########################
MultLoop:
	andi $t2, $t0, 1
	
        beq $t2, 0, ifZero
	
	add $t3, $t3, $t1

	ifZero:
		sll $t1, $t1, 1
		srl $t0, $t0, 1

	addi $t4, $t4, 1

	beq $t4, 16, ExitMultLoop
	j MultLoop

ExitMultLoop:
	li $v0, 4
	la $a0, product
	syscall

	li $v0, 1
	move $a0, $t3
	syscall

#########################
# Compute a/b
#########################
	move $t0, $s0	#remainder
	move $t1, $s1	#divisor
	li $t2, 0	#quotient
	li $t3, 0	#counter

	sll $t1, $t1, 16		#shifts divisor to the left part of the 16 bit number
        beq $t1, 0, DivOneZero
		
DivLoopOne:
	#sub $t0, $t0, $t1
	
	blt $t0, $t1, RemOneNegative
		sub $t0, $t0, $t1
		sll $t2, $t2, 1
		ori $t2, $t2, 1		#set LSB of quotient to 1
		j RemOnePositive
	RemOneNegative:
                #add $t0, $t0, $t1	#add divisor back to remainder
		sll $t2, $t2, 1
		#set LSB of quotient to zero by shifting right then back left
		#srl $t2, $t2, 1
		#sll $t2, $t2, 1
		
		
	RemOnePositive:

        srl $t1, $t1, 1
	addi $t3, $t3, 1
	beq $t3, 17, ExitDivLoopOne
	j DivLoopOne

ExitDivLoopOne:
	#Print Quotient
	li $v0, 4
	la $a0, quotient1
	syscall

	li $v0, 1
	move $a0, $t2
	syscall

	#Print Remainder
	li $v0, 4
	la $a0, remOne
	syscall
	
	li $v0, 1
	move $a0, $t0
	syscall
	j StartDivTwo

#Handle Divide by zero exception
DivOneZero:
	#Print Quotient
	li $v0, 4
	la $a0, quotient1
	syscall

	la $v0, 4
	la $a0, divByZero
	syscall	

StartDivTwo:
#########################
# Compute b/a
#########################
	move $t0, $s1	#remainder
	move $t1, $s0	#divisor
	li $t2, 0	#quotient
	li $t3, 0	#counter

	sll $t1, $t1, 16		#shifts divisor to the left part of the 16 bit number
        beq $t1, 0, DivTwoZero
DivLoopTwo:
	#sub $t0, $t0, $t1
	
	blt $t0, $t1, RemTwoNegative
		sub $t0, $t0, $t1
		sll $t2, $t2, 1
		ori $t2, $t2, 1		#set LSB of quotient to 1
		j RemTwoPositive
	RemTwoNegative:
                #add $t0, $t0, $t1	#add divisor back to remainder
		sll $t2, $t2, 1
		#set LSB of quotient to zero by shifting right then back left
		#srl $t2, $t2, 1
		#sll $t2, $t2, 1
		
		
	RemTwoPositive:

        srl $t1, $t1, 1
	addi $t3, $t3, 1
	beq $t3, 17, ExitDivLoopTwo
	j DivLoopTwo

ExitDivLoopTwo:
	#Print Quotient
	li $v0, 4
	la $a0, quotient2
	syscall

	li $v0, 1
	move $a0, $t2
	syscall

	#Print Remainder
	li $v0, 4
	la $a0, remTwo
	syscall
	
	li $v0, 1
	move $a0, $t0
	syscall
        j Exit

DivTwoZero:
	#Print Quotient
	li $v0, 4
	la $a0, quotient2
	syscall

	li $v0, 4
	la $a0, divByZero
	syscall

Exit:
	li $v0, 4
	la $a0, Goodbye
	syscall

	li $v0,10
	syscall
	
